package com.xls.resource.dialog;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ImageView;

import com.lzz.bottomencapsulation.consts.NetConsts;
import com.lzz.bottomencapsulation.entity.VersionEntity;
import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.manager.DownloadApkManage;
import com.lzz.bottomencapsulation.popupwindow.CenterBaseDialog;
import com.lzz.bottomencapsulation.utils.AppUtils;
import com.lzz.bottomencapsulation.utils.PermissionUtil;
import com.xls.resource.R;

import org.greenrobot.eventbus.EventBus;

/***
 @package ${package}
 @filename ${版本升级}
 */
public class EditionUpDialog extends CenterBaseDialog implements View.OnClickListener {

    VersionEntity editionEntity;

    ImageView editionBtnCancel;

    /**
     * @param context
     */
    public EditionUpDialog(Activity context) {
        super(context, true);
        setCanceledOnTouchOutside(false);//设置点击Dialog以外的地方不能让Dialog消失
    }

    public void setVersionEntity(VersionEntity editionEntity) {
        this.editionEntity = editionEntity;
    }

    @Override
    protected int getView() {
        return R.layout.dialog_edittion;
    }

    @Override
    public void initView() {
        editionBtnCancel=findViewById(R.id.edition_close);
        editionBtnCancel.setOnClickListener(this);
        findViewById(R.id.edition_btn_ok).setOnClickListener(this);
        if (editionEntity.isForceUpdate()) {
            editionBtnCancel.setVisibility(View.GONE);
        } else {
            editionBtnCancel.setVisibility(View.VISIBLE);
        }
    }

    public PermissionUtil.PermissionGrant permissionGrant = new PermissionUtil.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            if (requestCode == PermissionUtil.CODE_DOWLODE_APK) {
                DownloadApkManage.getInstance().downloadAPK(getContext(), editionEntity);
                EventBus.getDefault().post(new MessageEvent(NetConsts.CODE_50001,editionEntity));
                dismiss();
            } else if (requestCode == -PermissionUtil.CODE_DOWLODE_APK) {
                AlertDialog alertDialog = new AlertDialog.Builder(context).setMessage
                        ("该权限是应用升级必须权限，拒绝将无法使用请前往设置").setPositiveButton(context.getResources().getString(R
                        .string.confirm), new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        AppUtils.startAppSetting(context);
                    }
                }).create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.setCancelable(false);
                alertDialog.show();
            }
        }
    };

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.edition_close) {
            dismiss();
        } else if (id == R.id.edition_btn_ok) {
            PermissionUtil.requestPermission(context, PermissionUtil.CODE_DOWLODE_APK, permissionGrant);
        }
    }
}
